#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back

using namespace std;

typedef long long ll;
typedef double db;
typedef vector < int > vec;
typedef pair < int , int > pii;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 510;
const int M = 1e5 + 10;

namespace F { // max_flow
    const int N = 1e3 + 10;
    const int M = 2e5 + 10;
    const int inf = 0x3f3f3f3f;
    int h[N],now[N],d[N],cnt = 1,S,T;
    struct edge { int v,w,nxt; } e[M << 1];
    inline void tlink(int x,int y,int w) { e[++cnt] = (edge) { y,w,h[x] }; h[x] = cnt; }
    inline void link(int x,int y,int w) { tlink(x,y,w); tlink(y,x,0); }
    inline void setst(int s,int t) { S = s; T = t; }
    queue < int > q;
    bool bfs() {
	while(q.size()) q.pop(); q.push(S); memset(d,0,sizeof d); d[S] = 1; now[S] = h[S];
	while(!q.empty()) {
	    int x = q.front(); q.pop();
	    for(int i = h[x],y;i;i = e[i].nxt)
		if(e[i].w && !d[y = e[i].v]) {
		    d[y] = d[x] + 1; q.push(y); now[y] = h[y];
		    if(y == T) return 1;
		}
	} return 0;
    }
    int dinic(int x,int flow) {
	if(x == T) return flow;
	int res = flow;
	for(int i = now[x],y;i && res;i = e[i].nxt) {
	    now[x] = i;
	    if(e[i].w && d[y = e[i].v] == d[x] + 1) {
		int k = dinic(y,min(res,e[i].w));
		if(!k) d[y] = 0; e[i].w -= k; e[i ^ 1].w += k; res -= k;
	    }
	}
	return flow - res;
    }
    ll runit() {
	ll maxflow = 0,flow;
	while(bfs()) while(flow = dinic(S,inf)) maxflow += flow;
	return maxflow;
    }
};

int n,m,c[N];
bool vis[N];
ll dis[N];
vector < pii > G[N];

void dijk() {
    memset(dis,0x3f,sizeof dis); dis[1] = 0;
    priority_queue < pair < ll , int > ,vector < pair < ll , int > > , greater < pair < ll ,int > > > q;
    q.push({0,1});
    while(!q.empty()) {
	int x = q.top().se; q.pop(); if(vis[x]) continue; vis[x] = 1;
	for(auto d : G[x])
	    if(dis[d.fi] > dis[x] + d.se) {
		dis[d.fi] = dis[x] + d.se; q.push({dis[d.fi],d.fi});
	    }
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
    n = in,m = in;
    for(int i = 1,u,v,w;i <= m;i++) u = in,v = in,w = in,G[u].pb({v,w}),G[v].pb({u,w});
    for(int i = 1;i <= n;i++) c[i] = in;
    dijk();
    for(int x = 1;x <= n;x++) {
	for(auto d : G[x]) {
	    int y = d.fi,w = d.se;
	    if(dis[y] == dis[x] + w) F :: link(x + n,y,F :: inf);
	}
    }
    for(int x = 1;x <= n;x++) F :: link(x,x + n,c[x]);
    F :: setst(n + 1,n);
    printf("%lld\n",F :: runit());
    return 0;
}
